package com.obsidian.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.obsidian.content.entity.CourseBase;
import com.obsidian.content.mapper.CourseBaseMapper;
import com.obsidian.content.model.CourseBaseParam;
import com.obsidian.content.model.PageParams;
import com.obsidian.content.model.PageResult;
import com.obsidian.content.model.dto.QueryCourseParamsDto;
import com.obsidian.content.service.ICourseBaseService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * <p>
 * 课程基本信息 服务实现类
 * </p>
 *
 * @author jianghao
 * @since 2023-02-06
 */
@Service
public class CourseBaseServiceImpl extends ServiceImpl<CourseBaseMapper, CourseBase> implements ICourseBaseService {

    /**
     * 课程查询
     *
     * @param params               params
     * @param queryCourseParamsDto queryCourseParamsDto
     * @return PageResult<CourseBase>
     */
    @Override
    public PageResult<CourseBase> queryCourseBaseList(PageParams params, @RequestBody QueryCourseParamsDto queryCourseParamsDto) {
        Page<CourseBase> page = new Page<>(params.getPageNo(), params.getPageSize());
        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNoneEmpty(queryCourseParamsDto.getCourseName()), CourseBase::getName, queryCourseParamsDto.getCourseName());
        Page<CourseBase> pageResult = baseMapper.selectPage(page, queryWrapper);
        List<CourseBase> records = pageResult.getRecords();
        long total = pageResult.getTotal();
        return new PageResult<>(records, total, params.getPageNo(), params.getPageSize());
    }

    /**
     * 分页查询
     *
     * @return Page<CourseBase>
     */
    @Override
    public Page<CourseBase> lisePage(CourseBaseParam courseBaseParam, int currentPage, int currentSize) {
        Page<CourseBase> page = new Page<>(currentPage, currentSize);
        LambdaQueryWrapper<CourseBase> wrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(courseBaseParam.getName())) {
            wrapper.like(CourseBase::getName, courseBaseParam.getName());
        }
        return baseMapper.selectPage(page, wrapper);
    }
}
